﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Security.Cryptography;
using System.Net.Mail;
using System.Net;
using System.Globalization;

namespace FuckData.Common
{
    public class FunctionUtil
    {
        /// <summary>
        /// Split string
        /// </summary>
        /// <param name="start">Start index char</param>
        /// <param name="cout">Count char</param>
        /// <returns></returns>
        public static string SplitString(string start, int cout)
        {
            if (start != null)
            {
                string final = String.Empty;
                if (cout > start.Length)
                    final = start;
                else
                    for (int i = 0; i < start.Length; i++)
                        if (i < cout)
                            final += start[i].ToString();
                        else
                        {
                            while (start[i].ToString() != " ")
                            {
                                final += start[i];
                                i++;
                                if (i >= start.Length)
                                    break;
                            }
                            final = final + " ...";
                            break;
                        }
                return final;
            } return "";
        }

        /// <summary>
        /// Convert to number standar
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static string ConverNumberDetail(int number)
        {
            string final = String.Empty;
            string num = number.ToString();
            int temp = num.Length - 2;
            if (number.ToString().Length < 4)
                final = number.ToString();
            else
                for (int i = num.Length; i > 0; i--)
                {
                    final = num[i - 1].ToString() + final;
                    if (i == temp && i != 1)
                    {
                        final = "." + final;
                        temp = temp - 3;
                    }
                }
            return final;
        }


        /// <summary>
        /// Convert string to MD5
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        public static string GetMD5(string Input)
        {
            string str_md5 = "";
            if (Input != null)
            {
                byte[] mang = System.Text.Encoding.UTF8.GetBytes(Input);

                MD5CryptoServiceProvider my_md5 = new MD5CryptoServiceProvider();
                mang = my_md5.ComputeHash(mang);

                foreach (byte b in mang)
                    str_md5 += b.ToString("X2");
            }
            return str_md5;
        }

        public static DateTime ConvertDateTimeToDate(DateTime? Input)
        {
            DateTime ngaysinh = (DateTime)Input;
            string Output = ngaysinh.ToString("dd/MM/yyyy");
            DateTime OutputDate = DateTime.ParseExact(Output, "dd/MM/yyyy", new CultureInfo("en-US"));
            return OutputDate;
        }

        public static string ConvertDateTime(DateTime Input)
        {
            if (Input != null)
            {
                DateTime ngaysinh = Input;
                string Output = ngaysinh.ToString("dd/MM/yyyy");
                return Output;
            } return "";
        }

        public static string ConvertDateTime(DateTime? Input)
        {
            if (Input != null)
            {
                DateTime ngaysinh = (DateTime)Input;
                string Output = ngaysinh.ToString("dd/MM/yyyy");
                return Output;
            } return "";
        }

        public static string ConvertDateTimeFull(DateTime Input)
        {
            if (Input != null)
            {
                DateTime ngaysinh = Input;
                string Output = ngaysinh.ToString("dd/MM/yyyy HH:mm:ss");
                return Output;
            }
            else return "";
        }

        public static string ConvertDateTimeFull(DateTime? Input)
        {
            if (Input != null)
            {
                DateTime ngaysinh = (DateTime)Input;
                string Output = ngaysinh.ToString("dd/MM/yyyy HH:mm:ss");
                return Output;
            }
            else return "";
        }

        public static string[] GetXmlData(int MatrixCount, string Path)
        {
            int i = 0;
            string[] xmlData = new string[MatrixCount];
            XmlTextReader textReader = new XmlTextReader(Path);
            while (textReader.Read())
            {
                XmlNodeType nType = textReader.NodeType;
                if (nType == XmlNodeType.Text)
                {
                    if (textReader.Value != "")
                    {
                        xmlData[i] = textReader.Value;
                        i++;
                    }
                }
            }
            textReader.Close();
            return xmlData;
        }

        public static string CreatePassword(int length)
        {
            string valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
            string res = "";
            Random rnd = new Random();
            while (0 < length--)
                res += valid[rnd.Next(valid.Length)];
            return res;
        }

        public static bool SendMail(string MailForm, string Password, string MailTo, string Head, string Body)
        {
            try
            {
                SmtpClient client = new SmtpClient();
                client.Host = "smtp.gmail.com";
                client.Port = 587;
                client.UseDefaultCredentials = false;
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.EnableSsl = true;
                client.Credentials = new NetworkCredential(MailForm, Password);
                var mail = new MailMessage();
                mail.From = new MailAddress(MailForm);
                mail.To.Add(MailTo);
                mail.Subject = Head;
                mail.Body = Body;
                client.Send(mail);
                return true;
            }
            catch { return false; }
        }

    }
}
